//
//  SinaHomeTableViewCell.swift
//  weibo
//
//  Created by sirius on 16/1/9.
//  Copyright © 2016年 sirius. All rights reserved.
//

import UIKit
import SnapKit

class SinaHomeTableViewCell: UITableViewCell {
    //声明一个全局的约束
    var bottomConstant: Constraint?
    //NVVM
    var statusViewModel: SinaStatusViewModel?{
        didSet{
            
            originalView.originalViewModel = statusViewModel
            
            footerView.footerViewModel = statusViewModel
            
            retweetView.backgroundColor = UIColor.clearColor()
            
            //卸载之前重用时的约束
            
            bottomConstant?.uninstall()
            
            if statusViewModel?.model?.retweeted_status == nil {
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstant = make.top.equalTo(originalView.snp_bottom).constraint
                })
                retweetView.hidden = true
                
            }else{
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstant = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                retweetView.hidden = false
                
                retweetView.retweetViewMode = statusViewModel
            }
            
            
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("error")
    }
    private func setupUI(){
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(footerView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(SinaHomeMargin)
            make.top.equalTo(contentView.snp_top).offset(SinaHomeMargin)
            make.width.equalTo(contentView.snp_width).offset(-SinaHomeMargin * 2)
            
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(SinaHomeMargin)
            make.top.equalTo(originalView.snp_bottom)
            make.width.equalTo(contentView.snp_width).offset(-SinaHomeMargin * 2)
        }
        footerView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.width.equalTo(contentView.snp_width)
            make.height.equalTo(44)
            // 区分 转发微博 约束
            bottomConstant  = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(footerView.snp_bottom)
            make.width.equalTo(self.snp_width)
            make.top.equalTo(self.snp_top)
        }
        
    }
    
    //懒加载
    lazy var originalView: SinaOriginalView = SinaOriginalView()
    lazy var retweetView: SinaRetweenView = SinaRetweenView()
    lazy var footerView: SinaFooterView = SinaFooterView ()
    
}
